package site.teamo.biu.flink.common.function.descriptor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;
import site.teamo.biu.flink.common.function.descriptor.subunit.SubUnitType;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

@Data
@Accessors(chain = true)
public class OperatorParameterDescriptor {
    private SubUnitType type;
    private String key;
    private String label;

    public static List<OperatorParameterDescriptor> generateList(List<JSONObject> operatorParameters) {
        List<OperatorParameterDescriptor> result = new LinkedList<>();
        if (CollectionUtil.isEmpty(operatorParameters)) {
            return result;
        }
        for (JSONObject json : operatorParameters) {
            String type = json.getString("type");
            if (StrUtil.isBlank(type)) {
                throw new RuntimeException("未配置的组件类型：" + json.toJSONString());
            }
            result.add((OperatorParameterDescriptor) json.to(SubUnitType.valueOf(type.toUpperCase(Locale.ROOT)).aClass));
        }
        return result;
    }

}